package jedis;

import redis.clients.jedis.Jedis;
import java.util.concurrent.TimeUnit;

public class TestString {

    public static void main(String[] args) {
//        Jedis jedis = new Jedis("127.0.0.1", 6379);
        Jedis jedis = JedisUtil.getJedis();

        jedis.flushDB();
        System.out.println("===========增加数据===========");
        System.out.println(jedis.set("key1","value1")); //OK
        System.out.println(jedis.set("key2","value2")); //OK
        System.out.println(jedis.set("key3", "value3")); //OK
        System.out.println("删除键key2:"+jedis.del("key2")); //删除键key2:1
        System.out.println("获取键key2:"+jedis.get("key2")); //获取键key2:null
        System.out.println("修改key1:"+jedis.set("key1", "value1Changed")); //修改key1:OK
        System.out.println("获取key1的值："+jedis.get("key1")); //获取key1的值：value1Changed
        System.out.println("在key3后面加入值："+jedis.append("key3", "End")); //在key3后面加入值：9
        System.out.println("key3的值："+jedis.get("key3")); //key3的值：value3End
        System.out.println("增加多个键值对："+jedis.mset("key01","value01","key02","value02","key03","value03")); //增加多个键值对：OK
        System.out.println("获取多个键值对："+jedis.mget("key01","key02","key03")); //获取多个键值对：[value01, value02, value03]
        System.out.println("获取多个键值对："+jedis.mget("key01","key02","key03","key04")); //获取多个键值对：[value01, value02, value03, null]
        System.out.println("删除多个键值对："+jedis.del("key01","key02")); //删除多个键值对：2
        System.out.println("获取多个键值对："+jedis.mget("key01","key02","key03")); //获取多个键值对：[null, null, value03]

        jedis.flushDB();
        System.out.println("===========新增键值对防止覆盖原先值==============");
        System.out.println(jedis.setnx("key1", "value1")); // 1
        System.out.println(jedis.setnx("key2", "value2")); // 1
        System.out.println(jedis.setnx("key2", "value2-new")); // 0
        System.out.println(jedis.get("key1"));  //value1
        System.out.println(jedis.get("key2")); //value2

        System.out.println("===========新增键值对并设置有效时间=============");
        System.out.println(jedis.setex("key3", 2, "value3")); //OK
        System.out.println(jedis.get("key3")); // value
        System.out.println("暂存时间" + jedis.ttl("key3")); //暂存时间2
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(jedis.get("key3")); // null

        System.out.println("===========获取原值，更新为新值==========");
        System.out.println(jedis.getSet("key2", "key2GetSet")); //value2
        System.out.println(jedis.get("key2")); //key2GetSet

        System.out.println("获得key2的值的字串："+jedis.getrange("key2", 2, 4)); //获得key2的值的字串：y2G

        jedis.close();
    }

}
